package main.Trees;

import com.primavera.common.value.ObjectId;
import main.Link.LinkTable;
import main.Link.SQLLink;
import main.SQL.FillSQLSmetModel;
import org.jdesktop.swingx.treetable.AbstractTreeTableModel;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;

public class SmetTreeModel extends AbstractTreeTableModel {
    private SmetNode root;
    NumberFormat formatter;
    private LinkTable lTable = null;
    private ObjectId curProj;

    Class[] types = new Class[]{
            Object.class, Object.class, Object.class, Double.class, Double.class, Double.class, Double.class, Double.class, Double.class, boolean.class, Object.class
    };

    public void openSmet(SmetNode node) {
        FillSQLSmetModel fssm = new FillSQLSmetModel();
        this.root = fssm.run(root, node);
    }

    boolean[] canEdit = new boolean[]{false, false, false, false, false, false, false, true};

    public void updateFact(ArrayList<SQLLink> links) {
        if (root.getChildren() == null || root.getChildCount() == 0) return;
        setNullFact(root);
        for (SQLLink link : links) {
            SmetNode smr = getSmRow(link.getSmrId(), root);
            if (smr != null) {
                smr.setFactor(smr.getFactor().add(link.getFact()));
                smr.setFactorFull(link.getFactFull());
                smr.setSmetHighlight();
            }
        }
    }

//    public void setFacts(HashMap<Integer, Double> facts) {
//        if (root.getChildCount() == 0) return;
//
//        Set<Integer> factsSet = facts.keySet();
//
//        for (int smrId : factsSet) {
//            SmetNode smr = getSmRow(smrId, root);
//            if (smr == null) continue;
//            smr.setFactor(facts.get(smrId));
//            smr.setReady(smr.getFactor() == 0 ? "" : "inProgress");
//        }
//    }
//
//    public void deleteFact(ArrayList<SQLLink> links) {
//        for (SQLLink link : links) {
//            SmetNode smr = getSmRow(link.getSmrId(), root);
//            if (smr != null) {
//                double val = smr.getFactor() - link.getFact();
//                smr.setFactor(val);
//                if (smr.getFactor() == 0) {
//                    smr.setReady("");
//                } else {
//                    smr.setReady("inProgress");
//                }
//            }
//        }
//    }

    public void nullHigh(SmetNode node) {
        if (node.getChildCount() != 0) {
            for (SmetNode sm : node.getChildren()) {
                nullHigh(sm);
            }
        } else {
            node.setHigh("");
        }
    }

    public SmetNode getSmRow(int id, SmetNode node) {
        SmetNode stn = null;
        if (node.getChildCount() == 0) {
            return null;
        }
        for (SmetNode sm : node.getChildren()) {
            if (sm.getId() == id && sm.isString) return sm;
            if (!sm.isString) stn = getSmRow(id, sm);
            if (stn != null) return stn;
        }
        return stn;
    }

    public void setNullFact(SmetNode curNode) {

        if (curNode.getChildCount() == 0) return;
        for (SmetNode smNode : curNode.getChildren()) {
            if (smNode.isString) {
                smNode.setFactor(new BigDecimal(0));
            } else {
                setNullFact(smNode);
            }
        }
    }

    public void closeSQLSmet(SmetNode node) {
        root.getChildren().remove(node);
    }

    @Override
    public Class getColumnClass(int columnIndex) {
        return types[columnIndex];
    }

    public SmetTreeModel() {
        root = new SmetNode("Сметы");
    }

    @Override
    public int getColumnCount() {
        return 11;
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "N";
            case 1:
                return "Наименование";
            case 2:
                return "Ед. Изм.";
            case 3:
                return "Кол-во";
            case 4:
                return "Стоимость";
            case 5:
                return "Трудозатраты";
            case 6:
                return "Механизмы";
            case 7:
                return "К";
            case 8:
                return "К общ";
            default:
                return "Unknown";
        }
    }

    @Override
    public Object getValueAt(Object node, int column) {

        SmetNode treenode = (SmetNode) node;
        switch (column) {
            case 0:
                return treenode.getNum();
            case 1:
                return treenode.getName();
            case 2:
                return treenode.getUnit();
            case 3:
                return treenode.getVolume();
            case 4:
                return treenode.getCost();
            case 5:
                return treenode.getHR();
            case 6:
                return treenode.getMR();
            case 7:
                return treenode.getFactor();
            case 8:
                return treenode.getFactorFull();
            case 9:
                return treenode.getHigh();
            case 10:
                return treenode.getReady();
            default:
                return "Unknown";
        }
    }

    @Override
    public Object getChild(Object node, int index) {
        SmetNode treenode = (SmetNode) node;
        return treenode.getChildAt(index);
//        return treenode.getChildren().get(index);
    }

    @Override
    public int getChildCount(Object parent) {
        SmetNode treenode = (SmetNode) parent;
        return treenode.getChildCount();
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        SmetNode treenode = (SmetNode) parent;
        for (int i = 0; i > treenode.getChildCount(); i++) {
            if (treenode.getChildAt(i) == child) {
                return i;
            }
        }

        return 0;
    }

    @Override
    public boolean isLeaf(Object node) {
        SmetNode treenode = (SmetNode) node;
        if (treenode.getChildCount() > 0) {
            return false;
        }
        return true;
    }

    @Override
    public SmetNode getRoot() {
        return root;
    }
}